﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SQLServerHealthCheck.Model;
using AG.Utilities;
using System.Collections.ObjectModel;

namespace SQLServerOptimizer.PL.WPF
{
    /// <summary>
    /// Interaction logic for IssueSettings.xaml
    /// </summary>
    public partial class IssueSettings //: UserControl
    {
        public ObservableCollection<IssueType> issuestypes { get; set; }
        HealthCheckDataContext dc;


        public IssueSettings()
        {
            InitializeComponent();
            testbox.ItemsSource = issuestypes;
            go();
        }
        public void go()//UserControl_Loaded(object sender, RoutedEventArgs e)
        {

            try
            {

                    dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionStringSSHCDatabase"));
                    issuestypes = new ObservableCollection<IssueType>();

                    var a = from i in dc.IssueTypes
                            orderby i.ScopeId, i.Description ascending
                            select i;

                    issuestypes = new ObservableCollection<IssueType>(a);
                    testbox.ItemsSource = issuestypes;

            }
            catch (Exception sql)
            {
                Logger.Error(sql);


            }
        }

   
        public void go(bool ischecked)//UserControl_Loaded(object sender, RoutedEventArgs e)
        {

            try
            {



                    foreach (var item in issuestypes)
                    {
                        item.Enabled = ischecked;
                    }


                    testbox.ItemsSource = issuestypes;
                }

            catch (Exception sql)
            {
                Logger.Error(sql);


            }
        }

        public void go(List<bool> scope, List<bool> catogory, List<bool> severity)//UserControl_Loaded(object sender, RoutedEventArgs e)
        {

            try
            {



                    dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionStringSSHCDatabase"));
                   issuestypes = new ObservableCollection<IssueType>();
               



                    var a = from i in dc.IssueTypes
                            orderby i.ScopeId, i.Description ascending

                            select i;
                    List<IssueType> tempissuestypes = new List<IssueType>();
                    List<IssueType> tempissuestypes2 = new List<IssueType>();
                    foreach (var item in a)
                    {
                        if (scope[0] == true)
                        {
                            tempissuestypes.Add(item);
                        }
                        else 
                        if (scope[item.ScopeId])
                        {
                            tempissuestypes.Add(item);

                        }
                    }
                    foreach (var item in tempissuestypes)
                    {
                        if (catogory[0] == true)
                        {
                            tempissuestypes2.Add(item);
                        }
                        if (catogory[item.CategoryId])
                        {

                            tempissuestypes2.Add(item);
                        }
                      
                    }
                    foreach (var item in tempissuestypes2)
                    {
                        if (severity[(int)item.Severity])
                        {
                            issuestypes.Add(item);
                        }
                    }
                  
                    testbox.ItemsSource = issuestypes;
                    testbox.Items.Refresh();
                    var main = App.Current.MainWindow as MainWindow;
                    bool ischecked = false;
             
                    foreach (var item in issuestypes)
                    {
                        if ((bool)item.Enabled)
                        {

                            ischecked = true;
                        }
                    }
                    main.slctall.IsChecked = ischecked;




            }
            catch (Exception sql)
            {
                Logger.Error(sql);


            }
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
            {
                var tresh = ((System.Windows.Controls.Panel)(((System.Windows.Controls.DockPanel)(((System.Windows.Controls.DockPanel)(((System.Windows.Controls.Primitives.ButtonBase)(sender)).Parent)).Parent)).FindName("threshold"))).Visibility;
                if (tresh == Visibility.Visible)
                {
                    ((System.Windows.Controls.Panel)(((System.Windows.Controls.DockPanel)(((System.Windows.Controls.DockPanel)(((System.Windows.Controls.Primitives.ButtonBase)(sender)).Parent)).Parent)).FindName("threshold"))).Visibility = Visibility.Collapsed;

                }
                else
                {
                    ((System.Windows.Controls.Panel)(((System.Windows.Controls.DockPanel)(((System.Windows.Controls.DockPanel)(((System.Windows.Controls.Primitives.ButtonBase)(sender)).Parent)).Parent)).FindName("threshold"))).Visibility = Visibility.Visible;

                }
            }
        }

        private void CheckBoxenable_Checked(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
            {
                // Query the database for the row to be updated.
                var query =
                    from iss in dc.IssueTypes
                    where iss.IssueTypeId == (int)(((System.Windows.FrameworkElement)(((System.Windows.Controls.Primitives.ToggleButton)(sender)).Parent)).Tag)
                    select iss;

                // Execute the query, and change the column values
                // you want to change.
                foreach (IssueType iss in query)
                {
                    iss.Enabled = true;

                    // Insert any additional changes to column values.
                }

                // Submit the changes to the database.
                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    // Provide for exceptions.
                }
                // ((System.Windows.FrameworkElement)(((System.Windows.Controls.Primitives.ToggleButton)(sender)).Parent)).Tag
            }
        }


        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (IsLoaded)
            {
                if (((System.Windows.Controls.Primitives.ToggleButton)(((System.Windows.Controls.DockPanel)(((System.Windows.FrameworkElement)(sender)).Parent)).FindName("tbtn"))).Visibility == Visibility.Visible)
                {
                    var tresh = ((System.Windows.Controls.Panel)(((System.Windows.Controls.DockPanel)(((System.Windows.Controls.DockPanel)(((System.Windows.FrameworkElement)(sender)).Parent)).Parent)).FindName("threshold"))).Visibility;
                    if (tresh == Visibility.Visible)
                    {
                        ((System.Windows.Controls.Panel)(((System.Windows.Controls.DockPanel)(((System.Windows.Controls.DockPanel)(((System.Windows.FrameworkElement)(sender)).Parent)).Parent)).FindName("threshold"))).Visibility = Visibility.Collapsed;
                        ((System.Windows.Controls.Primitives.ToggleButton)(((System.Windows.Controls.DockPanel)(((System.Windows.FrameworkElement)(sender)).Parent)).FindName("tbtn"))).IsChecked = false;
                    }
                    else
                    {
                        ((System.Windows.Controls.Panel)(((System.Windows.Controls.DockPanel)(((System.Windows.Controls.DockPanel)(((System.Windows.FrameworkElement)(sender)).Parent)).Parent)).FindName("threshold"))).Visibility = Visibility.Visible;
                        ((System.Windows.Controls.Primitives.ToggleButton)(((System.Windows.Controls.DockPanel)(((System.Windows.FrameworkElement)(sender)).Parent)).FindName("tbtn"))).IsChecked = true;
                    }
                }

            }
        }

        private void CheckBoxenable_Unchecked(object sender, RoutedEventArgs e)
        {
            if (IsLoaded)
            {
                var query =
                 from iss in dc.IssueTypes
                 where iss.IssueTypeId == (int)((System.Windows.Controls.DockPanel)(((System.Windows.Controls.Primitives.ToggleButton)(sender)).Parent)).Tag
                 select iss;

                // Execute the query, and change the column values
                // you want to change.
                foreach (IssueType iss in query)
                {
                    iss.Enabled = false;
                    // Insert any additional changes to column values.
                }

                // Submit the changes to the database.
                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    // Provide for exceptions.
                }
            }
        }

        private void cbtest_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //OPTIMIZE 
            if (IsLoaded)
            {
                try
                {



                    var query =
                     from iss in dc.IssueTypes
                     where iss.IssueTypeId == (int)(((System.Windows.FrameworkElement)(((System.Windows.Controls.Primitives.Selector)(sender)).Parent)).Tag)
                     select iss;

                    // Execute the query, and change the column values
                    // you want to change.
                    foreach (IssueType iss in query)
                    {
                        iss.Severity = ((System.Windows.Controls.Primitives.Selector)(sender)).SelectedIndex + 1;

                        // Insert any additional changes to column values.
                    }
                }
                catch (Exception)
                {

                    throw;
                }
                // Submit the changes to the database.
                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    // Provide for exceptions.
                }
            }
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (IsLoaded)
            {
                try
                {
                    var query =
                       from iss in dc.ThresholdValues
                       where iss.ThresholdValueID == (int)((System.Windows.Controls.Primitives.TextBoxBase)(sender)).Tag
                         && iss.IssueTypeID == (int)((System.Windows.Controls.StackPanel)(((System.Windows.Controls.Primitives.TextBoxBase)(sender)).Parent)).Tag
                       select iss;

                    // Execute the query, and change the column values
                    // you want to change.
                    foreach (ThresholdValue iss in query)
                    {
                        iss.Value = Convert.ToInt32(((System.Windows.Controls.TextBox)(sender)).Text);

                        // Insert any additional changes to column values.
                    }
                }
                catch { }
                // Submit the changes to the database.
                try
                {
                    dc.SubmitChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    // Provide for exceptions.
                }
            }
        }


    }
}
